package com.nepenthe.view;

import com.nepenthe.Utils.MybatisUtils;
import com.nepenthe.Utils.UserGuiUtils;
import com.nepenthe.dao.UserMapper;
import com.nepenthe.pojo.User;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;

public class login {
    /**
     * Mybatis映射对象
     */
//    UserMapper mapper = MybatisUtils.getsqlSession().getMapper(UserMapper.class);
    JLabel a1 = new JLabel("学号");
    JTextField username = new JTextField();
    JLabel a2 = new JLabel("密   码");
    JPasswordField password = new JPasswordField();
    JButton okbtn = new JButton("登陆");
    JButton exitbtn = new JButton("返回");

    public void loginInit() {
        JFrame jFrame = UserGuiUtils.userGuiInit();
        UserMapper mapper = MybatisUtils.getMapper();
        init(jFrame, mapper);
        //设置窗体可见
        jFrame.setVisible(true);
    }


    public void init(JFrame jFrame, UserMapper mapper) {
        /*标题部分--North*/
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new FlowLayout());
        titlePanel.add(new JLabel("登录界面"));
        jFrame.getContentPane().add(titlePanel, "North");

        /*输入部分--Center*/
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(null);
        a1.setBounds(50, 20, 50, 20);
        a2.setBounds(50, 60, 50, 20);
        fieldPanel.add(a1);
        fieldPanel.add(a2);
        username.setBounds(110, 20, 120, 20);
        password.setBounds(110, 60, 120, 20);
        fieldPanel.add(username);
        fieldPanel.add(password);
        jFrame.getContentPane().add(fieldPanel, "Center");

        //确认按钮事件
        okbtn.addActionListener(e -> {
            if (e.getSource() == okbtn) {
                if (mapper.queryUser(username.getText(), new String(password.getPassword())) != null) {
                    User user = mapper.queryUser(username.getText(), new String(password.getPassword()));
                    jFrame.dispose();
                    new userInfo().userInfoInit(user);
                } else {
                    System.out.println("登陆失败");
                }
            }
        });
        //返回按钮事件
        exitbtn.addActionListener(e -> {
            jFrame.dispose();
            new index().indexInit();
        });

        /*按钮部分--South*/
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2, 1));
        buttonPanel.add(okbtn);
//        okbtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        buttonPanel.add(exitbtn);
//        exitbtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
        jFrame.getContentPane().add(buttonPanel, "South");
    }
}

